<?php
    /*
    * Copyright (c) 2007, Osuvaldo Ramos
    * 
    * All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    *
    *    * Redistributions of source code must retain the above copyright
    *      notice, this list of conditions and the following disclaimer.
    *    * Redistributions in binary form must reproduce the above copyright
    *      notice, this list of conditions and the following disclaimer in the
    *      documentation and/or other materials provided with the distribution.
    *    * Neither the name of the CoreTech nor the names of its
    *      contributors may be used to endorse or promote products derived from
    *      this software without specific prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * 
    * $Id: install.php 2 2008-01-21 02:48:28Z xdracox $
    */
    define('BNETCS', true);
    define('ROOT', '..');
    require '../common.php';
    
    include_once ROOT.'/mods/RankSystem/'.$CONF['mods']['ranksystem']['name'].
        '.php';
    
    function install_header()
    {
        echo<<<END
<html>
    <head>
        <title>Install BnetCS</title>
    </head>
    <body>
END;
    }
    
    function install_footer()
    {
        echo<<<END
    </body>
</html>
END;
    }
    
    install_header();
    
    if ( isset($_POST['commander']) && isset($_POST['cpassword'])
        && isset($_POST['confirm']) )
    {
        $errors = array();
        
        if ( $_POST['cpassword'] != $_POST['confirm'] )
            $errors[] = 'Passwords did not match.';
        
        if ( sizeof($errors) > 0 )
        {
            print '<p>Some errors were found...</p>';
            print '<ul>';
            foreach ( $errors as $err )
                print '<li>'.$err.'</li>';
        }
        else
        {
            $member_name = $_POST['commander'];
            $plain_pw = $_POST['cpassword'];
            $pw = salt_password($plain_pw, $CONF['auth']['salt1'],
                $CONF['auth']['salt2']);
            
            $add = array('member_join_date' => time());
            
            $auth->addUser($member_name, $pw, $add);
            
            // get the new member's id
            $sql = sprintf('SELECT member_id
                FROM bnetcs_members
                WHERE member_login = "%s"', $member_name);
            $result = $mdb2->query($sql);
            $row = $result->fetchRow();
            RankSystem::addUser($row['member_id'], true);
            
            print '<h1>Congratulations!</h1>';
            print '<p>BnetCS is now installed. Please make sure to delete the entire install/ directory.</p>';
        }
    }
    else
    {
        
        echo<<<END
        <h1>BnetCS Installation</h1>
        
        <p><b>Make sure you have read the INSTALL file. You should have already
        created your database tables and setup your config.php file!!!</b></p>
        
        <p>You should have also already installed custom mods that are part
        of the "built-in" mod system (i.e. RankSystem, FrontPage, etc.)</p>
        
        <p>Enter the following information to complete installation.</p>
        
        <form action="install.php" method="post">
            <table>
                <tr>
                    <td>Administrator username:</td>
                    <td><input type="text" name="commander" maxlength="255" size="32" /></td>
                </tr>
                <tr>
                    <td>Administrator password:</td>
                    <td><input type="password" name="cpassword" maxlength="255" size="32" /></td>
                </tr>
                <tr>
                    <td>Confirm password:</td>
                    <td><input type="password" name="confirm" maxlength="255" size="32" />
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td><button type="submit">Submit</button></td>
                </tr>
            </table>
        </form>
END;
    }
    
    install_footer();
?>
